package com.crane.oss.web;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.crane.common.core.dto.SessParty;
import com.crane.common.core.dto.SessUser;
import com.crane.common.core.exception.AppException;
import com.crane.common.core.exception.BasicErrorCode;
import com.crane.common.core.exception.BizException;
import com.crane.common.framework.shiro.ShiroUtils;
import com.crane.oss.sys.bean.resp.PartyBean;
import com.crane.oss.sys.service.PartyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@Controller
@Slf4j
public class BaseBusinessController {

    @Resource
    protected HttpServletRequest httpServletRequest;

    @Resource
    protected HttpServletResponse httpServletResponse;

    @Autowired
    protected HttpSession session;

    @Autowired
    protected PartyService partyService;


    protected SessUser getUser() {
        return ShiroUtils.getUser();
    }

    protected Long getUserId() {
        return getUser().getUserId();
    }

    protected Long getPartyId() {
    	return getParty().getPartyId();
    }

    protected SessParty getParty() {
        return getSessParty();
    }

    protected void setParty(SessParty party) {
        ShiroUtils.setParty(party);
    }

    /**
     * <p>
     * 判断sessionParty中用户角色的状态为有效
     * </p>
     *
     * @return
     * @throws BizException
     */
    protected SessParty getSessParty() {
        SessParty sessParty = ShiroUtils.getParty();
        if (sessParty == null) {
            return null;
        }
        try {
            PartyBean partyBean = partyService.queryPartyByCode(sessParty.getPartyCode());
            if (partyBean != null) {
                return sessParty;
            }
        } catch (BizException e) {
            throw new AppException(BasicErrorCode.SESSION_TIMEOUT);
        }
        throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "机构");
    }

    /**
     * 格式化参数
     *
     * @param data      请求的json串
     * @param paramName 请求json串中的参数key
     * @return
     * @throws BizException
     */
    protected String getParamter(String data, String paramName) {
        if (data == null || StringUtils.isBlank(data)) {
            throw new BizException();
        }
        try {
            JSONObject jsonObj = JSONObject.parseObject(data);
            String tmp = jsonObj.getString(paramName);
            if (StringUtils.isBlank(tmp)) {
                throw new BizException();
            }
            return tmp;
        } catch (Exception ex) {
            throw new BizException();
        }
    }

    @SuppressWarnings("deprecation")
	public String getIpAddr(HttpServletRequest request) {
        String ip = null;
        try {
            ip = request.getHeader("x-forwarded-for");
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
        } catch (Exception e) {
            log.error("IPUtils ERROR ", e);
        }
        return ip;
    }

}
